﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace WindowsGame1
{
    class ResourceManager
    {
        public static readonly ResourceManager Instance = new ResourceManager();

        ContentManager contentLoader;
        Dictionary<string, Texture2D> textures;

        private ResourceManager()
        {
            textures = new Dictionary<string, Texture2D>();
        }

        public Texture2D Load(string name)
        {
            Texture2D texture = contentLoader.Load<Texture2D>(name);
            if (texture == null)
                return texture;
            textures.Add(name, texture);
            return texture;
        }

        public void RegisterLoader(ContentManager content)
        {
            contentLoader = content;
        }

        public bool GetResource(string key, out Texture2D value)
        {
            bool result = true;

            textures.TryGetValue(key, out value);
            if (value == null)
            {
                Texture2D texture = Load(key);
                if (texture == null)
                {
                    result = false;
                }
                value = texture;
            }

            return result;
        }

        public bool RemoveTextureResource(string key)
        {
            return textures.Remove(key);
        }
    }
}
